﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.Collections.ObjectModel;
using DDF.Entity;

namespace ARR.UI.Models
{
    public interface IArticleModel : IModelBase
    {
        ObservableCollection<Article> Articles { get; set; }

        ObservableCollection<AttributeType> AttributeTypes { get; set; }

        ObservableCollection<Country> Countries { get; set; }

        ObservableCollection<Category> Categories { get; set; }

        Article CurrentArticle { get; set; }

        void DeleteArticles(List<Article> SelectedArticles);

        void Save();

        void LoadArticles();

    }

    public class ArticleModel : ModelBase, IArticleModel
    {
        public ArticleModel()
        {
            Articles = new ObservableCollection<Article>();
            Countries = new ObservableCollection<Country>();
            Categories = new ObservableCollection<Category>();
            AttributeTypes = new ObservableCollection<AttributeType>();
        }

        public ObservableCollection<Article> Articles
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ObservableCollection<AttributeType> AttributeTypes
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ObservableCollection<Country> Countries
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ObservableCollection<Category> Categories
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Article CurrentArticle
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void DeleteArticles(List<Article> SelectedArticles)
        {
            throw new NotImplementedException();
        }

        public void Save()
        {
            throw new NotImplementedException();
        }

        public void LoadArticles()
        {
            throw new NotImplementedException();
        }

    }

    public class ArticleModelMock : MockModelBase, IArticleModel
    {
        public ArticleModelMock()
        {
            Articles = new ObservableCollection<Article>();
            Countries = new ObservableCollection<Country>();
            Categories = new ObservableCollection<Category>();
            AttributeTypes = new ObservableCollection<AttributeType>();

            LoadArticles();
            LoadAttributeTypes();
            LoadCountries();
            LoadCategories();
        }


        public ObservableCollection<Article> Articles { get; set; }

        public ObservableCollection<AttributeType> AttributeTypes { get; set; }

        public ObservableCollection<Country> Countries { get; set; }

        public ObservableCollection<Category> Categories { get; set; }

        public Article CurrentArticle { get; set; }


        public void DeleteArticles(List<Article> selectedArticles)
        {
            selectedArticles.ForEach(st =>
            {
                if (Articles.Contains(st))
                    Articles.Remove(st);
            });
        }

        public void Save()
        {
            if (CurrentArticle.Id == 0)
            {
                int newID = Articles.Max(art => art.Id) + 1;
                CurrentArticle.Id = newID;

                Articles.Add(CurrentArticle);
            }
            else
            {
                List<Article> aux = Articles.ToList();
                Articles = new ObservableCollection<Article>(aux);
            }

            CurrentArticle = null;
        }

        public void LoadArticles()
        {
            Articles.Clear();
            
            Articles.Add(new Article { Id = 1, Code = "AR01", EAN = "AR01", Name = "Articulo 1", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });
            Articles.Add(new Article { Id = 2, Code = "AR02", EAN = "AR02", Name = "Articulo 2", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });
            Articles.Add(new Article { Id = 3, Code = "AR03", EAN = "AR03", Name = "Articulo 3", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });
            Articles.Add(new Article { Id = 4, Code = "AR04", EAN = "AR04", Name = "Articulo 4", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });
            Articles.Add(new Article { Id = 5, Code = "AR05", EAN = "AR05", Name = "Articulo 5", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });
            Articles.Add(new Article { Id = 6, Code = "AR06", EAN = "AR06", Name = "Articulo 6", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });
            Articles.Add(new Article { Id = 7, Code = "AR07", EAN = "AR07", Name = "Articulo 7", CategoryId = 1, CountryId = 1, Brand = "Brand", Manufacturer = "Manufacturer", Attributes = new List<Entities.Attribute>() });

            foreach (Article art in Articles)
            {
                art.ChangeState(EntityState.Original);
            }
        }

        private void LoadCategories()
        {
            Categories.Add(new Category { Id = 1, Code = "Cat01", Name = "Categoria 1", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 2, Code = "Cat02", Name = "Categoria 2", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 3, Code = "Cat03", Name = "Categoria 3", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 4, Code = "Cat04", Name = "Categoria 4", Level = 1, Convention = "NACS" });
            Categories.Add(new Category { Id = 5, Code = "Cat05", Name = "Categoria 5", Level = 1, Convention = "NACS" });
        }

        private void LoadCountries()
        {
            Countries.Add(new Country { Id = 1, Code = "C1", Name = "Country1" });
            Countries.Add(new Country { Id = 2, Code = "C2", Name = "Country2" });
            Countries.Add(new Country { Id = 3, Code = "C3", Name = "Country3" });
            Countries.Add(new Country { Id = 22, Code = "C4", Name = "Country4" });
        }
        
        private void LoadAttributeTypes()
        {
            AttributeTypes.Add(new AttributeType { Id = 1, Name = "ConsumptionType", Values = new List<Entities.Attribute>() });
            AttributeTypes.Add(new AttributeType { Id = 2, Name = "SizeSegment", Values = new List<Entities.Attribute>() });
            AttributeTypes.Add(new AttributeType { Id = 3, Name = "Flavour", Values = new List<Entities.Attribute>() });

            AttributeTypes[0].Values.Add(new Entities.Attribute { Id = 1, AttributeTypeId = 1, AttributeValue = "Immediate" });
            AttributeTypes[0].Values.Add(new Entities.Attribute { Id = 2, AttributeTypeId = 1, AttributeValue = "Future" });

            AttributeTypes[1].Values.Add(new Entities.Attribute { Id = 3, AttributeTypeId = 2, AttributeValue = "Single" });
            AttributeTypes[1].Values.Add(new Entities.Attribute { Id = 4, AttributeTypeId = 2, AttributeValue = "Multipack" });

            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 5, AttributeTypeId = 3, AttributeValue = "Apple" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 6, AttributeTypeId = 3, AttributeValue = "Chocolate" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 7, AttributeTypeId = 3, AttributeValue = "Almond" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 8, AttributeTypeId = 3, AttributeValue = "Strawberry" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 9, AttributeTypeId = 3, AttributeValue = "Honey" });
        }

    }
}
